<?php

namespace backend\controllers;

use Yii;
use common\models\customInfo\CustomInfo;
use common\models\customInfo\CustomInfoSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use zhongkefu\helpers\Utils;
use dosamigos\qrcode\QrCode;
use zhongkefu\helpers\SwfiEmail;
/**
 * CustomInfoController implements the CRUD actions for CustomInfo model.
 */
class CustomInfoController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all CustomInfo models.
     * @return mixed
     */
    public function actionIndex($id)
    {
        $searchModel = new CustomInfoSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams,$id);
        //调用函数查找所有表单元素
        $custom_elements_wap = Utils::bdelements($id);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'custom_elements_wap'=>$custom_elements_wap,
            'id'=>$id,
        ]);
    }

    /**
     * Displays a single CustomInfo model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new CustomInfo model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new CustomInfo();
        //调用函数查找所有表单元素
        $custom_elements_wap = Utils::bdelements(Yii::$app->request->get()['id']);
        $custominfos = json_decode($custom_elements_wap);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->info_id]);
        } else {
            if(!empty($custominfos))
            {
                return $this->render('create', [
                'model' => $model,
                'custom_elements_wap'=>$custom_elements_wap,'id'=>Yii::$app->request->get()['id']]);
            }
            else
            {
                header("Content-type: text/html; charset=utf-8");
                echo "<script>alert('请选择表单类型');history.go(-1);history.go(-1);</script>";
                exit;
            }
            
        }
    }

    /**
     * Updates an existing CustomInfo model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) ) {
            if($model->save())
            {
                return $this->redirect(['view', 'id' => $model->info_id]);
            }
            
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing CustomInfo model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $this->findModel($id)->delete();
        return $this->redirect(['index','id'=>$model->ac_id]);
    }

    /**
     * Finds the CustomInfo model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return CustomInfo the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = CustomInfo::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    //添加会议报名信息到数据库
    public function actionAddcustom()
    {
        $id = Yii::$app->request->post()['ac_id'];
        $searchModel = new CustomInfoSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams,$id);
        //调用函数查找所有表单元素
        $custom_elements_wap = Utils::bdelements($id);

         $setinfo = Yii::$app->db->createCommand('SELECT * FROM activity where ac_id='.$id)->queryOne();
         if(Yii::$app->request->post())
         {
            $sub_info = Utils::eleadd($id);
            $addinfo = Yii::$app->db->createCommand()->insert('custom_info', [ 'add_time' => time(), 'sub_info' => $sub_info,'ac_id'=>$id])->execute();
            $nextid = Yii::$app->db->getLastInsertID();
            if($addinfo > 0)
            {
                return $this->redirect(['/custom-info/bmsuccess','id'=>$nextid,'ac_id'=>Yii::$app->request->post()['ac_id']]);
            }
            else
            {
                 Yii::$app->session->setFlash('success', Yii::t('app', '报名失败！！'));
            }
         }
        //var_dump(Yii::$app->request->get());
        
    }
    //会议报名第二步 确认信息 $id为info—id,ac_id为活动id
    public function actionConfirm($id,$ac_id)
    {
        $setinfo = Yii::$app->db->createCommand('SELECT * FROM activity where ac_id='.$ac_id)->queryOne();
        $list = Utils::eleinfo($id);
        if(Utils::isMobile())
        {
            return $this->render('mconfirm',['list'=>$list,'setinfo'=>$setinfo,'bd_id'=>$id]);
        }
        else
        {
            return $this->render('confirm',['list'=>$list,'setinfo'=>$setinfo,'bd_id'=>$id]);
        }
        
    }
    //会议报名第三步 提交成功
    public function actionBmsuccess(){
        //$ac_id为活动id，bd——id为表单id
        $bd_ids = Yii::$app->request->get()['id'];
        $ac_ids = Yii::$app->request->get()['ac_id'];
        if(Yii::$app->request->post())
        {
            $ac_id = Yii::$app->request->post()['ac_id'];
            $bd_id = Yii::$app->request->post()['bd_id'];
            $status = Yii::$app->request->post()['status'];
            $setinfo = Yii::$app->db->createCommand('SELECT * FROM activity where ac_id='.$ac_id)->queryOne();
            $list = Utils::eleinfo($bd_id);
            $upstatus = Yii::$app->db->createCommand('UPDATE custom_info SET status='.$status.' WHERE info_id='.$bd_id)->execute();
            $cusinfo = Yii::$app->db->createCommand('SELECT * FROM custom_info where info_id='.$bd_id)->queryOne();
            if($cusinfo['status']=='2')
            {
                //报名成功生成凭证码
                $pingzheng = rand(1000000,time());
                $pingzhengcode =  Yii::$app->request->hostInfo.'/code/zy/?code='.$pingzheng;
                QrCode::png($pingzhengcode,'upload/erm/'.$pingzheng.'.png', 'L', 20, 2); 
                Yii::$app->db->createCommand('UPDATE custom_info SET code='.$pingzheng.' WHERE info_id='.$bd_id)->execute();
                $subinfos = Yii::$app->db->createCommand("select sub_info from custom_info where info_id=".$bd_id)->queryAll();
                $subinfos =  unserialize($subinfos[0]['sub_info']);
                foreach ($subinfos as $key => $value) {
                   if($value['type']=="email")
                   {
                        $email = $value['value'];
                   }
                   if($value['name']=="姓名")
                   {
                        $name = $value['value'];
                   }
                   else
                   {
                        $name="匿名";
                   }
                }
                //报名成功发送邮件
                $mailer = SwfiEmail::getEmail();
                $mail= $mailer->compose();
                $mail->setTo($email);
                $mail->setSubject("恭喜！您已成功报名参加");
                $host = Yii::$app->request->hostInfo.'/code/zy/?code='.$pingzheng;
                $uname = $name;
                //$mail->setTextBody('Test conten1231123t');
                $content = '<p style="text-align: center;">
<span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 18px;">恭喜！您已成功报名参加&nbsp;'.$setinfo['ac_name'].'</span><span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 14px;"></span>
</p>
<hr/>
<p>
<span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 14px;"></span>
</p>
<p>
<span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 14px;"><br/></span>
</p>
<p>
<span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 14px;">尊敬的'.$uname.'</span><br/>
</p>
<p>
<span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 14px;"></span>
</p>
<p style="text-align: center;">
<span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 14px;"><a href="'.$host.'">这是您本次的参会凭证</a></span>
</p>
<p>
<span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 14px;">您本次的参会费用为</span>
</p>

<p style="text-align: right;">
<br/>
</p>
<hr/>
<p style="text-align: right;">
<span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 14px;">&nbsp; &nbsp;| &nbsp;由协会管理系统的技术支持.&nbsp;</span><br/>
</p>';
            $mail->setHtmlBody($content);//发布可以带html标签的文本
            if($mail->send()){
                Yii::$app->session->setFlash('success', '邮件发送成功！');
                return $this->redirect(['/custom-info/index?id='.$ac_id]);
            }else{
                Yii::$app->session->setFlash('error','邮件发送失败！');
            }
            //邮件发送完成
            }
        }
        //var_dump($cusinfo);die();
        return $this->render('bmsuccess',['list'=>$list,'setinfo'=>$setinfo,'bd_id'=>$bd_ids,'ac_id'=>$ac_ids]);  
        
    }
}
